<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Dubbo 服务引用 | flyingzc's blog</title><meta name="author" content="flyingzc"><meta name="copyright" content="flyingzc"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="官网 客户端服务引用客户端服务引用 demo12DemoService demoService &#x3D; (DemoService) context.getBean(&quot;demoService&quot;); &#x2F;&#x2F; get remote service proxydemoService.sayHello(&quot;world&quot;); dubbo 在客户端动态的生成了一个该 Interf">
<meta property="og:type" content="article">
<meta property="og:title" content="Dubbo 服务引用">
<meta property="og:url" content="https://flyingzc.github.io/articles/dubbo/dubbo04-refer.html">
<meta property="og:site_name" content="flyingzc&#39;s blog">
<meta property="og:description" content="官网 客户端服务引用客户端服务引用 demo12DemoService demoService &#x3D; (DemoService) context.getBean(&quot;demoService&quot;); &#x2F;&#x2F; get remote service proxydemoService.sayHello(&quot;world&quot;); dubbo 在客户端动态的生成了一个该 Interf">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://flyingzc.github.io/images/zc-icon.jpeg">
<meta property="article:published_time" content="2019-11-28T16:00:00.000Z">
<meta property="article:modified_time" content="2025-01-21T01:51:41.211Z">
<meta property="article:author" content="flyingzc">
<meta property="article:tag" content="Dubbo">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://flyingzc.github.io/images/zc-icon.jpeg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "Dubbo 服务引用",
  "url": "https://flyingzc.github.io/articles/dubbo/dubbo04-refer.html",
  "image": "https://flyingzc.github.io/images/zc-icon.jpeg",
  "datePublished": "2019-11-28T16:00:00.000Z",
  "dateModified": "2025-01-21T01:51:41.211Z",
  "author": [
    {
      "@type": "Person",
      "name": "flyingzc",
      "url": "https://flyingzc.github.io/"
    }
  ]
}</script><link rel="shortcut icon" href="/images/zc-icon.jpeg"><link rel="canonical" href="https://flyingzc.github.io/articles/dubbo/dubbo04-refer.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="DentpX3LnP_WNgEeFnNIcTZZky-Bby-zL-uI_9GqTAw"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: '',
  enable_page_level_ads: 'true'
});</script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Dubbo 服务引用',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/self/github-dark.css"><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">flyingzc's blog</span></a><a class="nav-page-title" href="/"><span class="site-name">Dubbo 服务引用</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">Dubbo 服务引用</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-11-28T16:00:00.000Z" title="发表于 2019-11-29 00:00:00">2019-11-29</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-01-21T01:51:41.211Z" title="更新于 2025-01-21 09:51:41">2025-01-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Dubbo/">Dubbo</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><p><a target="_blank" rel="noopener" href="https://dubbo.apache.org/zh-cn/docs/source_code_guide/refer-service.html">官网</a></p>
<h1 id="客户端服务引用"><a href="#客户端服务引用" class="headerlink" title="客户端服务引用"></a>客户端服务引用</h1><h2 id="客户端服务引用-demo"><a href="#客户端服务引用-demo" class="headerlink" title="客户端服务引用 demo"></a>客户端服务引用 demo</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">DemoService</span> <span class="hljs-variable">demoService</span> <span class="hljs-operator">=</span> (DemoService) context.getBean(<span class="hljs-string">&quot;demoService&quot;</span>); <span class="hljs-comment">// get remote service proxy</span><br>demoService.sayHello(<span class="hljs-string">&quot;world&quot;</span>);<br></code></pre></td></tr></table></figure>
<p>dubbo 在客户端动态的生成了一个该 Interface 类型的代理类.在这个代理类中封装了远程服务调用的组件.</p>
<h2 id="DemoService-创建"><a href="#DemoService-创建" class="headerlink" title="DemoService 创建"></a>DemoService 创建</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dubbo:reference</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;demoService&quot;</span> <span class="hljs-attr">group</span>=<span class="hljs-string">&quot;g1&quot;</span> <span class="hljs-attr">check</span>=<span class="hljs-string">&quot;true&quot;</span> <span class="hljs-attr">interface</span>=<span class="hljs-string">&quot;com.alibaba.dubbo.demo.DemoService&quot;</span></span><br><span class="hljs-tag">                     <span class="hljs-attr">client</span>=<span class="hljs-string">&quot;netty4&quot;</span> <span class="hljs-attr">timeout</span>=<span class="hljs-string">&quot;10000&quot;</span> <span class="hljs-attr">callbacks</span>=<span class="hljs-string">&quot;1000&quot;</span> <span class="hljs-attr">registry</span>=<span class="hljs-string">&quot;zk01&quot;</span> <span class="hljs-attr">filter</span>=<span class="hljs-string">&quot;demo2&quot;</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>Spring 容器启动时,AbstractBeanFactory.isFactoryBean() 会判断 demoService 配置的 RootBeanDefinition 的 beanType 为<code>com.alibaba.dubbo.config.spring.ReferenceBean</code>,它是个 FactoryBean,Spring 容器会创建 ReferenceBean 这个 FactoryBean 实例.</p>
<h2 id="获取-DemoService"><a href="#获取-DemoService" class="headerlink" title="获取 DemoService"></a>获取 DemoService</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">DemoService</span> <span class="hljs-variable">demoService</span> <span class="hljs-operator">=</span> (DemoService) context.getBean(<span class="hljs-string">&quot;demoService&quot;</span>); <span class="hljs-comment">// get remote service proxy</span><br></code></pre></td></tr></table></figure>
<p>会调用 factoryBean.getObject() 方法,即调用 ReferenceBean.getObject() 创建 demoService 实例.</p>
<p><a target="_blank" rel="noopener" href="https://github.com/apache/dubbo/pull/2754">DUBBO-issue-2757</a> debug 时候遇到这个 bug 了,需要在 idea 里设置下.</p>
<p><code>在服务提供方,Invoker 用于调用服务提供类.在服务消费方,Invoker 用于执行远程调用.</code></p>
<h2 id="客户端服务引用流程"><a href="#客户端服务引用流程" class="headerlink" title="客户端服务引用流程"></a>客户端服务引用流程</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">- refer<br>- context.getBean(&quot;demoService&quot;)<br>    - ReferenceBean.getObject():通过这个 FactoryBean 创建 demoService 实例<br>        - ReferenceConfig.init(): 调用父类ReferenceConfig.init()<br>            - ReferenceConfig.createProxy(map)创建DemoService代理对象<br>            - 1.远程引用的逻辑<br>            - 1.1.loadRegistries()加载注册中心URL数组<br>            - 1.2.引用服务invoker = refprotocol.refer(interfaceClass, urls.get(0))<br>                - Protocol$Adaptive.refer()<br>                    - ProtocolFilterWrapper.refer()<br>                        - ProtocolListenerWrapper.refer()<br>                            - RegistryProtocol.refer()<br>                            - 1.获取注册中心的url<br>                            - 2.获得注册中心registryFactory.getRegistry(url)<br>                            - 3.执行服务引用doRefer(cluster, registry, type, url)<br>                                - RegistryProtocol.doRefer()<br>                                - 1.根据DemoService的type和url创建RegistryDirectory 对象,并设置注册中心<br>                                - 2.向注册中心注册自己(服务消费者)registry.register()<br>                                - 3.向注册中心订阅 服务提供者 + 路由规则 + 配置规则<br>                                - 4.通过cluster创建Invoker对象cluster.join(directory)并返回Invoker<br>            - 1.3.生成invoker的代理对象并返回 return (T) proxyFactory.getProxy(invoker)<br>                - ProxyFactory$Adaptive().getProxy()<br>                    - JavassistProxyFactory.getProxy(invoker, interfaces)<br>                        - Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker))<br>                            - new InvokerInvocationHandler(invoker) // invoker为MockClusterInvoker实例<br></code></pre></td></tr></table></figure>

<h1 id="客户端服务调用"><a href="#客户端服务调用" class="headerlink" title="客户端服务调用"></a>客户端服务调用</h1><p>demoService.sayHello(“world”)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">- consumer-call<br>- demoService.sayHello(&quot;world&quot;)<br>    - com.alibaba.dubbo.common.bytecode.proxy0.sayHello(name) // 服务代理类<br>        - InvokerInvocationHandler.invoke(proxy, method, args)<br>        - 1.获取参数名和方法名<br>        - 2.RPC调用invoker.invoke(new RpcInvocation(method, args)).recreate()<br>            - 1.将method和参数封装成RpcInvocation对象<br>            - 2.MockClusterInvoker.invoke(invocation)<br>                - result = this.invoker.invoke(invocation)调用FailoverClusterInvoker.invoke(),先走父类AbstractClusterInvoker.invoke()<br>                    - 1.通过 directory 获得所有服务提供者 Invoker 集合RegistryDirectory.list(Invocation invocation)<br>                        - AbstractClusterInvoker.list(invocation)<br>                            - 1.AbstractDirectory.list(invocation)<br>                                - RegistryDirectory.doList(invocation)<br>                                - 1.从localMethodInvokerMap中根据methodName获取所有匹配的invokers<br>                            - 2.根据routers,调用router.route()筛选invokers<br>                                - MockInvokersSelector.route()<br>                                    - 对于普通invoker集合,调用MockInvokersSelector.getNormalInvokers(invokers)<br>                                        - 不包含 MockInvoker 的情况下，直接返回 `invokers` 集合<br>                            - 3.获取loadBalance,默认是RandomLoadBalance实例<br>                            - 4.doInvoke(invocation, invokers, loadbalance)执行调用<br>                                - FailoverClusterInvoker.doInvoke()中<br>                                - 1.获取最大可调用次数: 最大可重试次数 + 1<br>                                - 2.select(loadbalance, invocation, copyinvokers, invoked)根据负载均衡机制从 copyinvokers 中选择一个Invoker<br>                                    - AbstractClusterInvoker.doselect()<br>                                    - 1.若只有一个invoker直接选择<br>                                    - 2.如果只有两个Invoker,轮循取<br>                                    - 3.其余loadbalance.select(invokers, getUrl(), invocation)使用loadbalance获取invoker对象<br>                                        - // doSelect()具体的loadbalance算法实现<br>                                - 3.将选中的invoker添加到invoked(保存已经调用过的invokers)中,并设置到context里<br>                                - 4.发起RPC调用并返回result,invoker.invoke(invocation)<br>                                    - invokerWrapper.invoke(invocation)<br>                                        - ProtocolFilterWrapper$1.invoke()后面会调用filter链<br>                            - 5.调用成功就直接返回result了,失败则根据重试次数和策略重新选中invoker调用<br></code></pre></td></tr></table></figure>
<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10/dist/style.css"><script src="https://fastly.jsdelivr.net/npm/d3@7"></script><script src="https://fastly.jsdelivr.net/npm/markmap-view@0.18.10"></script><script src="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10"></script>
<link rel="stylesheet" href="/css/markmap.css">

<script src="/js/markmap.js"></script>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io">flyingzc</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io/articles/dubbo/dubbo04-refer.html">https://flyingzc.github.io/articles/dubbo/dubbo04-refer.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://flyingzc.github.io" target="_blank">flyingzc's blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Dubbo/">Dubbo</a></div><div class="post-share"><div class="social-share" data-image="/images/zc-icon.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related" href="/articles/dubbo/dubbo01-overview.html" title="Dubbo 整理"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-11-26</div><div class="info-item-2">Dubbo 整理</div></div><div class="info-2"><div class="info-item-1">整理自官网, 对整体理解 Dubbo 很有作用. 作用Apache Dubbo 是一款高性能, 轻量级的 Java RPC 框架. 三大核心能力面向接口的远程方法调用 智能容错和负载均衡 以及服务自动注册和发现 健壮性监控中心宕掉不影响使用, 只是丢失部分采样数据. 数据库宕掉后, 注册中心仍能通过缓存提供服务列表查询, 但不能注册新服务. 注册中心对等集群, 任意一台宕掉后, 将自动切换到另一台. 注册中心全部宕掉后, 服务提供者 和 服务消费者 仍能通过本地缓存通讯. 服务提供者无状态, 任意一台宕掉后, 不影响使用. 服务提供者全部宕掉后, 服务消费者应用将无法使用, 并 无限次重连等待服务提供者恢复. 伸缩性注册中心为对等集群, 可动态增加机器部署实例, 所有客户端将自动发现新的注册中心. 服务提供者无状态, 可动态增加机器部署实例, 注册中心将推送新的服务提供者信息给消费者. 协议 Dubbo 协议采用 NIO 复用单一长连接, 并使用线程池并发处理请求, 减少握手和加大并发效率, 性能较好(推荐使用)在大文件传输时, 单一连接会成为瓶颈可用于生产环境  Rmi...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo02-spi.html" title="Dubbo SPI 机制"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-11-27</div><div class="info-item-2">Dubbo SPI 机制</div></div><div class="info-2"><div class="info-item-1">SPI 机制SPI 作用SPI 全称为 Service Provider Interface, 是一种服务发现机制. SPI 的本质是将接口实现类的全限定名配置在文件中, 并由服务加载器读取配置文件, 加载实现类. 这样可以在运行时, 动态为接口替换实现类. 正因此特性, 我们可以很容易的通过 SPI 机制为我们的程序提供拓展功能. Dubbo 里面有很多个组件, 每个组件在框架中都是以接口的形式抽象出来. 具体的实现又分很多种, 在程序执行时根据用户的配置来按需取接口的实现类. Dubbo 实现的 SPI 与 JDK 自带的 SPI 的区别摘自官网  JDK 标准的 SPI 会一次性实例化扩展点所有实现, 如果有扩展实现初始化很耗时, 但如果没用上也加载, 会很浪费资源.  如果扩展点加载失败, 连扩展点的名称都拿不到了.比如: JDK 标准的 ScriptEngine, 通过 getName() 获取脚本类型的名称, 但如果 RubyScriptEngine 因为所依赖的 jruby.jar 不存在, 导致 RubyScriptEngine 类加载失败,...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo03-export.html" title="Dubbo 服务暴露"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-11-29</div><div class="info-item-2">Dubbo 服务暴露</div></div><div class="info-2"><div class="info-item-1">Dubbo 与 Spring 的结合代码在 dubbo-config 下的 dubbo-config-spring 模块中  META-INF&#x2F;dubbo.xsddubbo.xsd 定义了 dubbo 自定义标签 META-INF&#x2F;spring.schemasspring.schemas 指定了 dubbo.xsd 文件的位置. 1http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd  META-INF&#x2F;spring.handlers关联 DubboNamespaceHandler 命名空间处理器和 dubbo.xsd 中的 targetNamespace,即...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo05-cluster.html" title="Dubbo Cluster"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-04</div><div class="info-item-2">Dubbo Cluster</div></div><div class="info-2"><div class="info-item-1">clusterCluster 将 Directory 中的多个 Invoker 伪装成一个 Invoker,对上层透明,伪装过程包含了容错逻辑,调用失败后,重试另一个. 集群模块处于服务提供者和消费者之间,对于服务消费者来说,集群可向其屏蔽服务提供者集群的情况,使其能够专心进行远程调用. cluster 继承结构 12345678910Cluster(com.alibaba.dubbo.rpc.cluster)    MergeableCluster(com.alibaba.dubbo.rpc.cluster.support)    FailfastCluster(com.alibaba.dubbo.rpc.cluster.support)    MockClusterWrapper(com.alibaba.dubbo.rpc.cluster.support.wrapper)    FailsafeCluster(com.alibaba.dubbo.rpc.cluster.support)   ...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo06-provider.html" title="Dubbo Provider处理请求"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-05</div><div class="info-item-2">Dubbo Provider处理请求</div></div><div class="info-2"><div class="info-item-1">服务端接收并响应请求流程provider处理请求流程  123456789101112- NettyServerWorker线程接收请求    - ByteToMessageDecoder.channelRead()        - ByteToMessageDecoder.callDecode()            - NettyCodecAdapter.InternalDecoder.decode()                - DubboCountCodec.decode()解码        - fireChannelRead(ctx, out, size)            - NettyServerHandler.channelRead()                - AbstractPeer.received()                    - MultiMessageHandler.received()                        - HeartbeatHandler.received()            ...</div></div></div></a><a class="pagination-related" href="/articles/dubbo/dubbo07-heartbeat.html" title="Dubbo心跳机制"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-15</div><div class="info-item-2">Dubbo心跳机制</div></div><div class="info-2"><div class="info-item-1">Dubbo心跳机制Dubbo 默认客户端和服务端都会发送心跳报文,用来保持 TCP 长连接状态. 在客户端和服务端,Dubbo 都会开启一个线程 循环扫描 并 检测连接是否超时. 在服务端发现超时则会主动关闭客户端连接. 在客户端发现超时则会主动重新创建连接. 默认心跳检测时间是 60 秒. 原理provider 心跳默认是在 heartbeat(一分钟)内如果没有读消息或写消息,就会发送心跳请求消息,如果连着3次(180s)没有读消息,provider会关闭channel. consumer 端的心跳默认是在 一分钟 内如果没有读消息或写消息,就会发送心跳请求消息,如果连着3次(三分钟)没有读消息,consumer会进行重连. 开启定时任务provider 在启动 netty时,在 HeaderExchangeServer 的构造方法中,会通过 startHeatbeatTimer() 启动心跳定时任务.consumer 在 HeaderExchangeClient 的构造方法里,同样通过 startHeatbeatTimer()...</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">flyingzc</div><div class="author-info-description">不积跬步无以至千里<br/>不积小流无以成江海</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/FlyingZC"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/FlyingZC" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">FlyingZC's Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%9C%8D%E5%8A%A1%E5%BC%95%E7%94%A8"><span class="toc-number">1.</span> <span class="toc-text">客户端服务引用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%9C%8D%E5%8A%A1%E5%BC%95%E7%94%A8-demo"><span class="toc-number">1.1.</span> <span class="toc-text">客户端服务引用 demo</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DemoService-%E5%88%9B%E5%BB%BA"><span class="toc-number">1.2.</span> <span class="toc-text">DemoService 创建</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-DemoService"><span class="toc-number">1.3.</span> <span class="toc-text">获取 DemoService</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%9C%8D%E5%8A%A1%E5%BC%95%E7%94%A8%E6%B5%81%E7%A8%8B"><span class="toc-number">1.4.</span> <span class="toc-text">客户端服务引用流程</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">客户端服务调用</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite03-transaction-begin.html" title="Apache Ignite 开启事务执行流程">Apache Ignite 开启事务执行流程</a><time datetime="2025-10-10T16:00:00.000Z" title="发表于 2025-10-11 00:00:00">2025-10-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite02-transaction.html" title="Apache Ignite 分布式事务原理">Apache Ignite 分布式事务原理</a><time datetime="2025-09-17T16:00:00.000Z" title="发表于 2025-09-18 00:00:00">2025-09-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite01-build.html" title="Apache Ignite IDEA 环境搭建">Apache Ignite IDEA 环境搭建</a><time datetime="2025-09-10T16:00:00.000Z" title="发表于 2025-09-11 00:00:00">2025-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/druid/druid01-resultset-leak.html" title="ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析">ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/jmeter/jmeter01-shardingsphere.html" title="Jmeter 压测 ShardingSphere">Jmeter 压测 ShardingSphere</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2025 By flyingzc</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>